#import "BWCrossStitch.h"
#import "BWThread.h"

@implementation  BWCrossStitch

- (BWCrossStitch *) initWithRow: (int) r
                         column: (int) c
                      topThread: (BWThread *) k1
                   bottomThread: (BWThread *) k2
                        divider: (BWThread_StitchDivider) d
                            tag: (int) tg
{
    if (self = [super init]) {
        row = r;
        column = c;
        topThread = [k1 retain];
        bottomThread = [k2 retain];
        divider = d;
        tag = tg;
    }

    return (self);

} // initWithRow+everything and cheese


- (BWCrossStitch *) initWithRow: (int) r
                         column: (int) c
                      topThread: (BWThread *) k1
                   bottomThread: (BWThread *) k2
                        divider: (BWThread_StitchDivider) d
{
    if (self = [self initWithRow: r
                     column: c
                     topThread: k1
                     bottomThread: k2
                     divider: d
                     tag: -1]) {
    }

    return (self);

} // initWithRow


- (BWCrossStitch *) initWithRow: (int) r
                         column: (int) c
                         thread: (BWThread *) t
                            tag: (int) tg
{
    if (self = [self initWithRow: r
                     column: c
                     topThread: t
                     bottomThread: nil
                     divider: kDivider_None
                     tag: tg]) {
    }

    return (self);

} // initWithRow+tag


- (BWCrossStitch *) initWithRow: (int) r
                         column: (int) c
                         thread: (BWThread *) t
{
    if (self = [self initWithRow: r
                     column: c
                     thread: t
                     tag: -1]) {
    }

    return (self);

} // initWithRow


- (void) dealloc
{
    [topThread release];
    [bottomThread release];
    [super dealloc];
} // dealloc


- (int) row
{
    return (row);
} // row


- (int) column
{
    return (column);
} // column


- (BWThread *) topThread
{
    return (topThread);
} // topThread


- (BWThread *) bottomThread
{
    return (bottomThread);
} // bottomThread


- (BWThread_StitchDivider) divider
{
    return (divider);
} // divider


- (int) tag
{
    return (tag);
} // tag


- (NSString *) description
{
    NSString *description;
    description = [NSString stringWithFormat:
                                @"BWCrossStitch at (%d, %d) with threads (%@ %@), tag %d, divider %d",
                            row, column, topThread, bottomThread, tag, divider];

    return (description);

} // description


- (BOOL) isPartialStitch
{
    if (divider == kDivider_None) {
        return (NO);
    } else {
        return (YES);
    }

} // isPartialStitch


- (BWCrossStitch *) stitchByReplacingThread: (BWThread *) search
                                 withThread: (BWThread *) replace
                                        tag: (int) newTag
{
    BOOL change = NO;
    BWThread *top = topThread;
    BWThread *bottom = bottomThread;

    if (topThread == search) {
        top = replace;
        change = YES;
    }
    if (bottomThread == search) {
        bottom = replace;
        change = YES;
    }

    BWCrossStitch *stitch = nil;

    if (change) {
        stitch = [[BWCrossStitch alloc]
                     initWithRow: row
                     column: column
                     topThread: top
                     bottomThread: bottom
                     divider: divider
                     tag: newTag];
        [stitch autorelease];
    }

    return (stitch);

} // stitchByReplacingThread


- (void) encodeWithCoder: (NSCoder *) coder
{
    [coder encodeValueOfObjCType: @encode(int)
           at: &row];
    [coder encodeValueOfObjCType: @encode(int)
           at: &column];
    [coder encodeValueOfObjCType: @encode(BWThread_StitchDivider)
           at: &divider];
    [coder encodeValueOfObjCType: @encode(int)
           at: &tag];

    [coder encodeObject: topThread];
    [coder encodeObject: bottomThread];

} // encodeWithCoder


- (id) initWithCoder: (NSCoder *) coder
{
    if (self = [super init]) {
        [coder decodeValueOfObjCType: @encode(int)
               at: &row];
        [coder decodeValueOfObjCType: @encode(int)
               at: &column];
        [coder decodeValueOfObjCType: @encode(BWThread_StitchDivider)
               at: &divider];
        [coder decodeValueOfObjCType: @encode(int)
               at: &tag];

        topThread = [[coder decodeObject] retain];
        bottomThread = [[coder decodeObject] retain];
    }

    return (self);

} // initWithCoder


@end // BWCrossStitch


